package com.lekang.le.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

import com.lekang.common.core.domain.entity.SysUser;
import com.lekang.le.domain.OrderManagement;
import com.lekang.le.mapper.OrderManagementMapper;
import com.lekang.system.mapper.SysUserMapper;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lekang.le.mapper.RefundRecordMapper;
import com.lekang.le.domain.RefundRecord;
import com.lekang.le.service.IRefundRecordService;

/**
 * 退款记录Service业务层处理
 * 
 * @author yyh
 * @date 2025-10-14
 */
@Service
public class RefundRecordServiceImpl implements IRefundRecordService 
{
    @Autowired
    private RefundRecordMapper refundRecordMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private OrderManagementMapper orderManagementMapper;

    /**
     * 查询退款记录
     * 
     * @param id 退款记录主键
     * @return 退款记录
     */
    @Override
    public RefundRecord selectRefundRecordById(Long id)
    {
        RefundRecord refundRecord = refundRecordMapper.selectRefundRecordById(id);
        Long applicant = refundRecord.getApplicant();
        SysUser sysUser = sysUserMapper.selectUserById(applicant);
        refundRecord.setSysUser(sysUser);
        return refundRecord;
    }

    /**
     * 查询退款记录列表
     * 
     * @param refundRecord 退款记录
     * @return 退款记录
     */
    @Override
    public List<RefundRecord> selectRefundRecordList(RefundRecord refundRecord)
    {
        List<RefundRecord> refundRecords = refundRecordMapper.selectRefundRecordList(refundRecord);
        for(RefundRecord rr : refundRecords){
            Long applicant = rr.getApplicant();
            SysUser sysUser = sysUserMapper.selectUserById(applicant);
            rr.setSysUser(sysUser);
        }
        return refundRecords;
    }

    /**
     * 新增退款记录
     * 
     * @param refundRecord 退款记录
     * @return 结果
     */
    @Override
    public int insertRefundRecordPro(String orderNo, Long applicant , String refundReason)
    {
        RefundRecord refundRecord = new RefundRecord();
        System.out.println("  orderNo" + orderNo + "  applicant" + applicant+"  refundReason"+refundReason);
        OrderManagement orderManagement = new OrderManagement();
        orderManagement.setOrderNo(orderNo);

        List<OrderManagement> orderManagements = orderManagementMapper.selectOrderManagementList(orderManagement);
        for(OrderManagement oo : orderManagements){
            Long orderId = oo.getId();
            oo.setOrderStatus("5");
            oo.setTransactionStatus("2");
            orderManagementMapper.updateOrderManagement(oo);
            BigDecimal price = oo.getOrderAmount();
            refundRecord.setRefundAmount(price);
        }
        int randomNum = ThreadLocalRandom.current().nextInt(1000, 10000); // 范围：[100000, 1000000)，即6位随机数
        refundRecord.setRefundNo("RF"+randomNum);
        Date now = new Date();
        refundRecord.setOrderNo(orderNo);
        refundRecord.setApplicant(applicant);
        refundRecord.setApplicationTime(now);
        refundRecord.setRefundTime(now);
        refundRecord.setRefundStatus("1");
        refundRecord.setRefundOrderStatus("0");
        refundRecord.setRefundReason(refundReason);
        refundRecord.setRefundMethod("原路返回");
        refundRecord.setRefundChannel("微信");



        return refundRecordMapper.insertRefundRecord(refundRecord);
    }
    @Override
    public int insertRefundRecord(RefundRecord refundRecord)
    {
        return refundRecordMapper.insertRefundRecord(refundRecord);
    }

    /**
     * 修改退款记录
     * 
     * @param refundRecord 退款记录
     * @return 结果
     */
    @Override
    public int updateRefundRecord(RefundRecord refundRecord)
    {
        return refundRecordMapper.updateRefundRecord(refundRecord);
    }

    /**
     * 批量删除退款记录
     * 
     * @param ids 需要删除的退款记录主键
     * @return 结果
     */
    @Override
    public int deleteRefundRecordByIds(Long[] ids)
    {
        return refundRecordMapper.deleteRefundRecordByIds(ids);
    }

    /**
     * 删除退款记录信息
     * 
     * @param id 退款记录主键
     * @return 结果
     */
    @Override
    public int deleteRefundRecordById(Long id)
    {
        return refundRecordMapper.deleteRefundRecordById(id);
    }
}
